<template>
  <!-- // 左侧的导航栏 -->
  <!-- 布局 -->
  <div>
    <el-container>
      <el-aside :style="{ width: isCollapse ? 'auto' : '200px' }">
        <navbar :menus="menus" :collapse="isCollapse"></navbar>
      </el-aside>
      <el-container>
        <el-header>
          <i
            class="el-collapse el-icon-s-fold"
            :class="isCollapse ? 'collapseTransform' : ''"
            @click="handleCollapse"
          ></i>

          <el-dropdown>
            <span class="el-dropdown-link">
              账户主页<i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item></el-dropdown-item>
              <el-dropdown-item>我的信息</el-dropdown-item>
              <el-dropdown-item>修改密码</el-dropdown-item>
              <el-dropdown-item @click.native="handleLogout">注销</el-dropdown-item>
               <!-- @click.native 绑定原生事件 -->
            </el-dropdown-menu>
          </el-dropdown>
        </el-header>
        <el-main>
          <router-view></router-view>
        </el-main>
        <el-footer
          >Copyright © 2001 - 2021 AD. All Rights Reserved. 69科技
          版权所有</el-footer>
      </el-container>
    </el-container>
    <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
      <span>下一步就是注销用户了噢，请慎重慎重</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleConfirmLogout">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import routes from "../router/routes";
import Navbar from "../components/Navbar";
export default {
  components: {
    Navbar,
  },
  //  导航栏点击事件取反
  data() {
    return {
      isCollapse: false,
      dialogVisible:false,
    };
  },

  methods: {
    resolve(arr, parentPath = "") {
      let newArr = [];
      // 第一步就是遍历数组当中的每一个元素
      arr.forEach((route) => {
        let obj = Object.assign({}, route);

        // 当上级路径存在的时候，而且组后一位不为/，则后面添加/
        parentPath =
          parentPath && parentPath.lastIndexOf("/") !== parentPath.length - 1
            ? parentPath + "/"
            : parentPath;

        //总是给加上父级路径
        obj.path = parentPath + obj.path;

        // 第二步就是判断有无children属性，有的话就需要再进行一步的处理，如果没有就直接push到下一个数组中去了。
        if (obj.children && obj.children.length > 0) {
          obj.children = this.resolve(obj.children, obj.path);
        }
        // 如果当前的路由信息有hidden信息，那么就将其子节点提升上来

        if (obj.meta && obj.meta.hidden) {
          if (obj.children && obj.children.length > 0) {
            obj.children.forEach((item) => {
              let tmp = Object.assign({}, item);
              newArr.push(tmp);
            });
          }
        } else {
          newArr.push(obj);
        }
      });

      return newArr;
    },
    handleCollapse() {
      this.isCollapse = !this.isCollapse;
    },
    // 注销事件
    handleLogout(){
       console.log('测试一下');
       this.dialogVisible = true;
    },
    // 注销确定事件
    handleConfirmLogout(){
      this.dialogVisible = false;
      localStorage.removeItem('accessToken')
      localStorage.removeItem('refreshToken')
      this.$router.push('/login')
    }
  },
  computed: {
    menus() {
      let arr = this.resolve(routes);
      console.log(arr);
      return arr;
    },
  },
  // 导航栏旋转点击事件，循环取反
};
</script>

<style scoped>
/* 左侧布局 */
.el-aside {
  background-color: #545c64;
}
/* 顶部布局 */
.el-header {
  background-color: darkgray;
  display: flex;
  justify-content: space-between;
}
/* 中部布局 */
.el-main {
  background-color: #f0f9eb;
  min-height: 670px;
}
/* 底部布局 */
.el-footer {
  background-color: chartreuse;
  display: flex;
  /* 弹性布局容器 */
  align-items: center;
  margin-top: 10px;

  /* 居中 */
  justify-content: center;
  /* 在弹性盒对象中<div>元素中的各项周围都留有空白 */
}

/* 导航栏按钮旋转多少度 */
.collapseTransform {
  transform: rotate(-90deg);
}

/* 下拉菜单 */

.el-dropdown-link {
  cursor: pointer;
  color: darkgoldenrod;
}
.el-icon-arrow-down {
  font-size: 12px;
}
</style>

